package hedron.db.model;

import hedron.Application;
import hedron.db.DBManager;
import java.sql.Connection;
import java.sql.ResultSet;
import java.util.Date;

public class StoredStock extends AbstractModel {

	private long ROW_ID = 0;

	public long STORE_ID = 0;
	public long STOCK_ID = 0;
	public long CUSTOMER_ID = 0;
	public double PRICE_BUY = 0d;
	public double PRICE_SELL = 0d;
	public long QTY = 0;
	public String METRIC = EMPTY_STR;
	public Date DELIVERY_DATE = new Date();

	public String STORE_NAME = EMPTY_STR;
	public String STOCK_NAME = EMPTY_STR;
	public String GROUP_NAME = EMPTY_STR;
	public String C_NAME = EMPTY_STR;

	@Override
	public long getID() {
		return ROW_ID;
	}

	@Override
	public String getIDName() {
		return "ROW_ID";
	}

	@Override
	public String getTableName() {
		return "HSTORE";
	}

	public StoredStock() {}

	public StoredStock(ResultSet rs) {
		if (rs == null) {
			throw new IllegalArgumentException("ResultSet might not be null");
		}
		if (!load(rs)) {
			throw new IllegalArgumentException("cannot load from ResultSet");
		}
	}

	@Override
	public boolean load(ResultSet rs) {
		DBManager db = Application.DB;
		ROW_ID = db.getLong(rs, getIDName(), 0);
		if (ROW_ID == 0) {
			return false;
		}

		ROW_ID = db.getLong(rs, "ROW_ID");
		STORE_ID = db.getLong(rs, "STORE_ID");
		STORE_NAME = db.getString(rs, "STORE_NAME");
		STOCK_ID = db.getLong(rs, "STOCK_ID");
		STOCK_NAME = db.getString(rs, "STOCK_NAME");
		GROUP_NAME = db.getString(rs, "GROUP_NAME");
		CUSTOMER_ID = db.getLong(rs, "CUSTOMER_ID");
		C_NAME = db.getString(rs, "C_NAME");
		PRICE_BUY = db.getDouble(rs, "PRICE_BUY");
		PRICE_SELL = db.getDouble(rs, "PRICE_SELL");
		QTY = db.getLong(rs, "QTY");
		METRIC = db.getString(rs, "METRIC");
		DELIVERY_DATE = db.getDate(rs, "DELIVERY_DATE", new Date(0));
		return true;
	}

	@Override
	public boolean isValid() {
		final boolean result = STORE_ID > 0 && STOCK_ID > 0 && CUSTOMER_ID >= 0 && PRICE_BUY > 0 && PRICE_SELL > 0
				&& QTY > 0 && DELIVERY_DATE != null;
		return result;
	}

	@Override
	public boolean store(Connection conn) {
		StringBuilder sql = new StringBuilder();
		if (ROW_ID <= 0) {// insert
			ROW_ID = Application.ID.getNewID();
			sql.append("insert into ");
			sql.append(getTableName());
			sql.append("(ROW_ID, STORE_ID, STOCK_ID, CUSTOMER_ID, PRICE_BUY, PRICE_SELL, QTY, METRIC, DELIVERY_DATE)");
			sql.append(" values(?,?,?,?,?,?,?,?,?)");
			return db.ddlQuery(conn, sql.toString(), ROW_ID, STORE_ID, STOCK_ID, CUSTOMER_ID, PRICE_BUY, PRICE_SELL,
					QTY, METRIC, DELIVERY_DATE);
		} else {// update
			sql.append("update ");
			sql.append(getTableName());
			sql.append(" SET STORE_ID=?, STOCK_ID=?, CUSTOMER_ID=?, PRICE_BUY=?, PRICE_SELL=?, QTY=?, METRIC=?, DELIVERY_DATE=? ");
			sql.append(" where ROW_ID = ").append(ROW_ID);
			return db.ddlQuery(conn, sql.toString(), STORE_ID, STOCK_ID, CUSTOMER_ID, PRICE_BUY, PRICE_SELL, QTY,
					METRIC, DELIVERY_DATE);
		}
	}

	@Override
	public boolean delete(Connection conn) {
		if (ROW_ID > 0) {// delete
			StringBuilder sql = new StringBuilder();
			sql.append("delete from ");
			sql.append(getTableName());
			sql.append(" where ROW_ID = ").append(ROW_ID);
			return db.ddlQuery(conn, sql.toString());
		} else {
			return false;
		}
	}
}
